package com.opmsys.controller;

import com.opmsys.pojo.dto.TestPageDTO;
import com.opmsys.pojo.entity.Test;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名称TestController
 * 描述
 *
 * @version 1.0
 * @author:冉启呈2021015445
 * @datatime:2024-06-18 16:21
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    private TestService testService;


    /**
     * 新增化验报告
     * @param test
     * @return
     */
    @PostMapping
    public Result addTest(Test test) {
        if(test.getWaterContent()!=null && test.getSandContent()!=null &&test.getPh()!=null){
            testService.addTest(test);
            return Result.successWithMsg("新增成功");
        }
        else
            return Result.error("油样水分含量,油样含砂量,酸碱值需非空");
    }


    /**
     * 批量删除化验报告
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deletePumpingBatch(@RequestParam(required = false) List<Integer> ids) {
        for (Integer id : ids)
            testService.deleteTest(id);
        return Result.successWithMsg("删除成功");
    }


    /**
     * 更新化验报告
     * @param test
     * @return
     */
    @PutMapping
    public Result updateTest(Test test) {
        log.info("修改化验结果id：{}",test.id);
        testService.updateTest(test);
        return Result.successWithMsg("更新成功");
    }


    /**
     * 搜索化验报告
     * @param testPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> searchTest(TestPageDTO testPageDTO) {
        PageResult result=new PageResult();
        List<Test> tests=testService.getTestPageWithConditions(testPageDTO);
        List<Test> tests1=testService.getTestListWithConditions(testPageDTO);
        result.setTotal(tests1.size());
        result.setRecords(tests);
        return Result.success(result);
    }
}
